package com.qyhl.school.school.reporter.news;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SCHOOL_REPORTER_NEWS)
public class SchoolReporterNewsActivity extends BaseActivity implements SchoolReporterNewsContract.SchoolReporterNewsView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private SchoolReporterNewsPresenter mPresenter;

    private CommonAdapter<NewsBean> mAdapter;

    private String titleName;

    private String newsId = "0";

    private int page = 1;

    private String sectionId;

    private List<NewsBean> mDataList = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.activity_school_reporter_news;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new SchoolReporterNewsPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        titleName = getIntent().getStringExtra("title");
        sectionId = getIntent().getStringExtra("id");


        title.setText(titleName);

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter = new CommonAdapter<NewsBean>(this, R.layout.item_school_report_catalog, mDataList) {
            @Override
            protected void convert(ViewHolder holder, NewsBean newsBean, int i) {
                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(SchoolReporterNewsActivity.this)
                        .load(newsBean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                        .into(cover);

                holder.setText(R.id.title, newsBean.getTitle());
                TextView title = holder.getView(R.id.title);

                if (titleName.equals("小记者在行动")) {
                    holder.setVisible(R.id.tagName, false);
                    title.setMaxLines(2);
                } else {
                    title.setMaxLines(1);
                    holder.setVisible(R.id.tagName, true);
                    holder.setText(R.id.tagName, newsBean.getSummary());
                }


                holder.setText(R.id.scan_num, "观看量:" + newsBean.getHitCount());

                if (StringUtils.isNotEmpty(newsBean.getWriter())) {
                    holder.setText(R.id.writer, newsBean.getWriter());
                    holder.setVisible(R.id.writer, true);
                } else {
                    holder.setVisible(R.id.writer, false);
                }
            }
        });

        if (Integer.parseInt(sectionId) >= 1000000) {
            mPresenter.getData(sectionId, page + "");
        } else {
            mPresenter.getData(sectionId, newsId);
        }
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                if (Integer.parseInt(sectionId) >= 1000000) {
                    mPresenter.getData(sectionId, page + "");
                } else {
                    mPresenter.getData(sectionId, newsId);
                }
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                if (Integer.parseInt(sectionId) >= 1000000) {
                    page = 1;
                    mPresenter.getData(sectionId, page + "");
                } else {
                    newsId = "0";
                    mPresenter.getData(sectionId, newsId);
                }
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (Integer.parseInt(sectionId) >= 1000000) {
                    mPresenter.getData(sectionId, page + "");
                } else {
                    mPresenter.getData(sectionId, newsId);
                }
            }
        });

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                NewsBean bean = mDataList.get(position);
                switch (bean.getType()) {
                    case "1"://普通新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY, bundle);
                    }
                    break;
                    case "2"://图片新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        bundle.putString("section", bean.getCatalogID());
                        RouterManager.router(ARouterPathConstant.PICTURE_NEWS_ACTIVITY, bundle);
                    }

                    break;
                    case "3"://直播
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("newsId", bean.getID());
                        bundle.putString("liveType", bean.getLivetype());
                        RouterManager.router(ARouterPathConstant.LIVE_NORMAL_ACTIVITY, bundle);
                    }
                    break;
                    case "4"://标题新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        bundle.putString("title", bean.getTitle());
                        bundle.putString("url", bean.getRedirectURL());
                        RouterManager.router(ARouterPathConstant.ADV, bundle);
                    }
                    break;
                    case "5"://视频新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY, bundle);
                    }
                    break;
                    case "8"://互动直播
                        break;
                    case "101"://抓取新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.NEWS_CATCH, bundle);
                    }
                    break;
                    default:
                        break;

                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @OnClick(R2.id.back_btn)
    public void onClick() {
        finish();
    }

    @Override
    public void setData(List<NewsBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        if (Integer.parseInt(sectionId) >= 1000000) {
            page++;
        } else {
            try {
                newsId = beanList.get(beanList.size() - 1).getID();
            } catch (Exception e) {
                newsId = "-1";
            }
        }
        mDataList.addAll(beanList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(this)) {
                showToast("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                showToast("暂无更多小记者团活动！");
            } else {
                showToast(msg);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setReloadButtonText("点击重试~");
            if (!NetUtil.isNetworkAvalible(this)) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }
}
